package com.fishing.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("serial")
public class PageInfo<T> extends com.github.pagehelper.PageInfo<T> {
	private String pageStr;
	private HttpServletRequest request;
	private String action;
	
	public PageInfo() {
	}
	
	/**
     * 包装Page对象
     *
     * @param list
     */
    public PageInfo(List<T> list,HttpServletRequest request) {
    	super(list,8);
    	//setPageUrl(request);
    }
    
    public String isSelected(int size,int psize){
    	if(size == psize){
    		return "selected=\"selected\"";
    	}
    	return "";
    }
    
    public String getSelect(){
    	int [] sizes = new int[]{10,15,20,25,50,100};
    	StringBuffer sb = new StringBuffer();
    	sb.append("<select onchange=\"changePageSize(this);\">");
    	for (int i = 0; i < sizes.length; i++) {
			sb.append("<option value=\""+sizes[i]+"\" "+isSelected(sizes[i],this.getPageSize())+">"+sizes[i]+"</option>");
		}
    	sb.append("</select>");
    	return sb.toString();
    }
    
    /*
    public void setPageUrl(HttpServletRequest request) {
    	this.request = request;
    	
    	//请求Action
		String actionUrl = this.action;
		if(null == actionUrl || "".equals(actionUrl)){
			actionUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + request.getRequestURI();
		}
    	
    	StringBuffer sb = new StringBuffer();
    	sb.append("<div class=\"pageDiv\"><form id=\"pageForm999999\" action=\""+actionUrl+"\" method=\"post\">");
    	sb.append("<script>function pageSubmit(pageNum,pageSize){document.getElementById(\"pageNum999999\").value=pageNum;" +
    	        "document.getElementById(\"pageSize999999\").value=pageSize;" +
    			"document.getElementById(\"pageForm999999\").submit();};" +
    			"function changePageSize(sel){var index = sel.selectedIndex;" +
    			"var value = sel.options[index].value;pageSubmit(1,value);}</script>");
    	
    	for(Map.Entry<String,Object> entry: getParameterMap(request).entrySet()) {
			if(!"pageSize".equals(entry.getKey()) && !"pageNum".equals(entry.getKey())){
				sb.append("<input type=\"hidden\" name=\""+entry.getKey()+"\" value=\""+entry.getValue().toString()+"\"/>");
			}
		}
    	
    	sb.append("<input type=\"hidden\" name=\"pageSize\" id=\"pageSize999999\" value=\""+this.getPageSize()+"\"/>");
    	sb.append("<input type=\"hidden\" name=\"pageNum\" id=\"pageNum999999\" value=\""+this.getPageNum()+"\"/>");
    	sb.append("<a href=\"javascript:pageSubmit('1','"+this.getPageSize()+"');\">首 页</a>");
    	if(this.isHasPreviousPage()){
    		sb.append("<a href=\"javascript:pageSubmit('"+this.getPrePage()+"','"+this.getPageSize()+"');\">上一页</a>");
    	}else{
    		sb.append("<a href=\"javascript:;\">上一页</a>");
    	}
    	for (int i = 0; i < this.getNavigatepageNums().length; i++) {
    		sb.append("<a "+(this.getPageNum()==(i+1)?"class=\"active\"":"")+" href=\"javascript:pageSubmit('"+this.getNavigatepageNums()[i]+"','"+this.getPageSize()+"');\">"+this.getNavigatepageNums()[i]+"</a>");
		}
    	
    	if(this.isHasNextPage()){
    		sb.append("<a href=\"javascript:pageSubmit('"+this.getNextPage()+"','"+this.getPageSize()+"');\">下一页</a>");
    	}else{
    		sb.append("<a href=\"javascript:;\">下一页</a>");
    	}
    	
    	sb.append("<a href=\"javascript:pageSubmit('"+this.getLastPage()+"','"+this.getPageSize()+"');\">尾页</a>");
    	sb.append("共"+this.getTotal()+"条记录</form></div>");
    	
    	this.pageStr = sb.toString();
    	
	}
    
    */
    public static Map<String,Object> getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map<String,String[]> properties = request.getParameterMap();
        // 返回值Map
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }

	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}

	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
}
